var ds;
var grid;
var insertForm;
var updateForm;
var form;
var win;

Ext.onReady(function (){
  
   initData();
});

function initData(){
    ds = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
            url: '../processYield.do?action=list'
        }),
        
        reader: new Ext.data.JsonReader({
            totalProperty: 'totalProperty',
            root: 'root'
        }, [{
            name: 'id'
        }, {
            name: 'specType'
        }, {
            name: 'processName'
        }, {
            name: 'procsCheck'
        }, {
            name: 'yield'
        }, {
            name: 'bz'
        }, {
            name: 'date1'
        }])
    });
    
    ds.load({
        params: {
            start: 0,
            limit: 10
        }
    });
    initGrid();
}

function getInsertForm(){
    if (!insertForm) {
        insertForm = new Ext.FormPanel({
            labelWidth: 95,
          	url: '../processYield.do?action=add',
            frame: true,
			method: 'post',
            bodyStyle: 'padding:5px 5px 0',
            width: 340,
            defaults: {
                width: 230
            },
            defaultType: 'textfield',
            standardSubmit: false,
            items: [{
                fieldLabel: '规格类型',
                name: 'specType',
                allowBlank: false
            }, {
                fieldLabel: '加工名称',
                name: 'processName',
				allowBlank: false 
            },{
                fieldLabel: '成品率约束',
                name: 'procsCheck',
                allowBlank: false
            }, {
                fieldLabel: '默认成品率',
                name: 'yield',
				allowBlank: false 
            }, {
                fieldLabel: '备注',
                name: 'bz',
				allowBlank: false 
            },{
				xtype : 'datefield',
				format : 'Y-m-d',
				fieldLabel: '日期',
                name: 'date1'
			}]
        });
    }
    return insertForm;
}



function getUpdateForm(){
    win.title = '更新汇率';
    if (!updateForm) {
        updateForm = new Ext.FormPanel({
            labelWidth: 95,
         	url: '../processYield.do?action=update',
			method: 'post',
            frame: true,
            bodyStyle: 'padding:5px 5px 0',
            width: 340,
            defaults: {
                width: 230
            },
            defaultType: 'textfield',
            standardSubmit: false,
           items: [{
                fieldLabel: '编号',
                name: 'id',
                allowBlank: false
            }, {
                fieldLabel: '规格类型',
                name: 'specType',
                allowBlank: false
            }, {
                fieldLabel: '加工名称',
                name: 'processName',
				allowBlank: false 
            },{
                fieldLabel: '成品率约束',
                name: 'procsCheck',
                allowBlank: false
            }, {
                fieldLabel: '默认成品率',
                name: 'yield',
				allowBlank: false 
            }, {
                fieldLabel: '备注',
                name: 'bz',
				allowBlank: false 
            },{
				xtype : 'datefield',
				format : 'Y-m-d',
				fieldLabel: '日期',
                name: 'date1'
			}]
        });
    }
    return updateForm;
}


function initGrid(){
    Ext.QuickTips.init();
    
    var xg = Ext.grid;
	
    // form
    if (!form) {
        form = getInsertForm();
    }

    // win
    if (!win) {
        win = new Ext.Window({
           // el: 'add-window',
            layout: 'fit',
            width: 380,
            height: 280,
            closeAction: 'hide',
            plain: true,
            items: form,
            buttons: [{
                text: '保存',
                handler: function(){
					if(form.getForm().isValid()){
						form.getForm().submit({						
	                        success: function(retForm,retAction){	                           
		                                Ext.MessageBox.alert('提示', '保存数据成功！');
		                                win.hide();
										ds.reload({
											params : {
												start : 0,
												limit : 10
											}
										});
	                            	},
	                        failure: function(retForm, retAction){      
								 		if (retAction.result && retAction.result == 'error') {
		                                    Ext.MessageBox.alert('提示', '保存数据失败！');
		                                }
		                                else {
		                                    Ext.MessageBox.alert('提示', '保存数据出错！');
		                                }
	                       			},
	                        waitMsg: '保存数据...'
	                    });					
					}
					else{
						Ext.MessageBox.alert('提示','请填入必需的数据');
					}
                }
            }, {
                text: '清空',
                handler: function(){
                    form.getForm().reset();
                }
            }, {
                text: '关闭',
                handler: function(){
					form.getForm().reset();
                    win.hide();
                }
            }]
        });
    }

    // Grid
    var sm = new xg.CheckboxSelectionModel();
    grid = new xg.GridPanel({
        id: 'button-grid',
        ds: ds,
        cm: new xg.ColumnModel([sm, {
            header: "编号",
            width: 40,
            sortable: true,
            dataIndex: 'id'
        }, {
            header: "规格类型",
            width: 40,
            sortable: true,
            dataIndex: 'specType'
        }, {
            header: "加工名称",
            width: 40,
            sortable: true,
            dataIndex: 'processName'
        }, {
            header: "成品率约束",
            width: 40,
            sortable: true,
            dataIndex: 'procsCheck'
        }, {
            header: "成品率",
            width: 40,
            sortable: true,
            dataIndex: 'yield'
        }, {
            header: "备注",
            width: 40,
            sortable: true,
            dataIndex: 'bz'
        }, {
			header: "日期",
			width: 40,
			sortable: true,
			dataIndex: 'date1'
		}]),
        sm: sm,
        stripeRows: true,
        
        viewConfig: {
            forceFit: true
        },
        
        // inline toolbars
        tbar: [{
            text: '添加',
            tooltip: '添加一条默认加工成品率信息',
            iconCls: 'add',
            onClick: function(){
                // update form to insert form
                form.hide();
				form = getInsertForm();
				form.show();
				var items = new Ext.util.MixedCollection();
				items.add("form", form);
				win.items = items;
				win.show(this);
				form.getForm().reset();
            }
        }, '-', {
            text: '修改',
            tooltip: '修改一条默认加工成品率信息',
            iconCls: 'option',
            onClick: function(){
                if (sm.getCount() == 1) {
                    // update form to update form
					form.hide();
					form = getUpdateForm();
					form.show();
					var items = new Ext.util.MixedCollection();
					items.add("form", form);
					win.items = items;
					win.show(this);
					form.getForm().reset();
					form.getForm().loadRecord(sm.getSelected());
                }
                else {
                    Ext.MessageBox.alert('提示', '请选择一条记录！');
                }
            }
        }, '-', {
            text: '删除',
            tooltip: '删除选择的默认加工成品率信息',
            iconCls: 'remove',
            onClick: function(){
                if (sm.getCount()==1) {
                    Ext.MessageBox.confirm('提示', '你确定要删除这些记录么?', function(button){
                        if (button == 'yes') {
                            var list = sm.getSelections();
                            
                            processYield.removeRecord(list[0].data["id"], function(data){
                                if (data > 0) {
                                    Ext.MessageBox.alert('提示', "删除" + data + '条数据成功!');
                                    ds.reload({
										params : {
											start : 0,
											limit : 10
										}
									});
                                }
                                else {
                                    Ext.MessageBox.alert('提示', "删除数据失败!");
                                }
                            });
                        }
                    });
                }
                else {
                    Ext.MessageBox.alert('提示', "请至少选择一条记录!");
                }
            }
        }],
        
        bbar: new Ext.PagingToolbar({
            pageSize: 10,
            store: ds,
            displayInfo: true,
            displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
            emptyMsg: "没有记录"
        }),
        width: 815,
        autoHeight: true,
        // frame:true,
        title: '列表',
        iconCls: 'icon-grid',
        renderTo: 'main'
    });
};

